import { tt } from "../../ttapi_interface/ttapi.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js"
import { UserData, UserState } from "../game.js";
import { BattleList } from "../model/battlelist.js";
import { State_MainMenu } from "./state_mainmenu.js";
import { State_Story } from "./state_story.js";



//可以直接在这里写出终极代码
//也可以多利用json配置
export class State_Logo extends UserState {

    private _inited: boolean = false;

    OnInit(statemgr: tt2.QFrame_App<UserData>): void {
        super.OnInit(statemgr);
        this.InitAsync();
    }
    OnExit(): void {
        let _ui = this.userdata.scene.GetUI();
        _ui.removeChildAll();
    }

    timer: number = 0;
    OnUpdate(delta: number): void {
        if (!this._inited)
            return;
        super.OnUpdate(delta);
        this.timer += delta;
        if (this.timer >= 2)
            this.timer = 0;

        let yadd = 0;

        if (this.timer > 1) {
            yadd = 2.0 - this.timer
        }
        else {
            yadd = this.timer;
        }

        let y = this.userdata.scene.GetUI().getWorldRect().Height / 2 + 20 * yadd;

        this._logo.localRect.setVPosByTopBorder(this._logo.sprite.totalHeight, y);

    }
    private _atlas: tt.Atlas;
    private _logoback: tt.Sprite;

    private _logo: tt2.QUI_Image
    private _rect: tt.Rectangle;
    private _btnborder: tt.Sprite;
    async InitAsync(): Promise<void> {

        //初始化UI
        this._logoback = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("logoback.png");

        let _imglogo = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("logo.png");

        this._btnborder = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("btnborder.png");
        this._atlas = await tt2.QFrame_ResMgr.LoadAtlasAsync("splittex_pack0.png.json");

        let img = new tt2.QUI_Image(this._logoback);
        img.localRect.setAsFill();
        let _ui = this.userdata.scene.GetUI();
        _ui.addChild(img);


        let _logo = this._logo = new tt2.QUI_Image(_imglogo);
        _logo.localRect.setHPosByCenter(_imglogo.totalWidth);
        let y = this.userdata.scene.GetUI().getWorldRect().Height / 2 + 10;
        _logo.localRect.setVPosByTopBorder(_imglogo.totalHeight, y);
        _ui.addChild(_logo);

        let label = new tt2.QUI_Label(this.app.font, "测试版本 001");
        label.halign = tt2.QUI_HAlign.Left;
        label.valign = tt2.QUI_VAlign.Top;
        label.color = new tt.Color(1, 0.4, 0.4);
        _ui.addChild(label);


        let btn = new tt2.QUI_Button();
        btn.localRect.setHPosByCenter(160);
        btn.localRect.setVPosByBottomBorder(40, 30);
        let bimg: tt2.QUI_ImageScale9;
        {
            let s9 = new tt2.QUI_Scale9(this._btnborder, new tt.Border(4, 4, 4, 4));
            let label = new tt2.QUI_Label(this.app.font, "开始游戏");
            label.fontScale.X = 2;
            label.fontScale.Y = 2;
            label.color = new tt.Color(0.3, 0.3, 0.3, 1);
            bimg = new tt2.QUI_ImageScale9(s9);
            label.localRect.setAsFill();
            bimg.addChild(label);
            bimg.localRect.setAsFill();
        }
        let bimg2 = bimg.Clone() as tt2.QUI_ImageScale9;
        bimg2.color = new tt.Color(0.5, 0.5, 0.5, 1.0);

        //bimg2.localRect.setAsFill();
        btn.ElemNormal = bimg;
        btn.ElemPress = bimg2;


        _ui.addChild(btn);
        btn.OnClick = () => {
            this.evt_ToStory();
        }


        await this.userdata.LoadAsync();
        this._inited = true;

    }
    //按钮事件，去StateStory
    evt_ToStory() {
        this.app.ChangeState(new State_MainMenu());
    }
}